package com.weds.edwx.job;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.utils.coder.Coder;
import com.weds.edwx.constant.LeCaiParams;
import com.weds.edwx.entity.DepResultContent;
import com.weds.edwx.entity.LeCaiDepEntity;
import com.weds.edwx.entity.LeCaiDepResp;
import com.weds.edwx.entity.TmpDtDepReq;
import com.weds.edwx.service.LeCaiDepUserService;
import com.weds.edwx.util.TimeUtil;
import com.weds.etl.base.SchedulerJob;
import com.weds.web.comm.entity.CommProcEntity;
import com.weds.web.comm.service.CommProcService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

public class LeCaiDepSchedulerJob extends SchedulerJob {

    private Logger log = LogManager.getLogger();

    @Resource
    private LeCaiParams leCaiParams;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private CommProcService commProcService;

    @Resource
    private LeCaiDepUserService leCaiDepUserService;

    @Override
    @Transactional
    public void execute(JobExecutionContext context) {
        //时间戳
        String stamp=TimeUtil.getNowTimeStamp();
        ///时间戳有效期三分钟
/*        Long s = (System.currentTimeMillis() - Long.parseLong(stamp)) / (1000 * 60);
        if(s>=3){
            stamp=TimeUtil.getNowTimeStamp();
        }*/
        //签名
        String sign="";
             if(leCaiParams.getDomain().equals("")){
                 sign=Coder.md5Hex(stamp+""+leCaiParams.getKey());
             }else{
                 sign=Coder.md5Hex(leCaiParams.getDomain()+""+stamp+""+leCaiParams.getKey());
             }
        //用户ID
        String companyId=leCaiParams.getCompanyId();
        //======组织参数============================================
        LeCaiDepEntity lcde=new LeCaiDepEntity();
        lcde.setCompanyId(companyId);
        lcde.setStamp(stamp);
        lcde.setSign(sign);
        //======发送请求============================================
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(lcde), headers);
        //System.out.println("请求参数="+request);
        String resp = restTemplate.postForObject(leCaiParams.getDepUrl(), request, String.class);
        //System.out.println("resppp=="+resp);
        LeCaiDepResp lcResp = JSONObject.parseObject(resp, LeCaiDepResp.class);
        //======处理响应结果============================================
        //请求超时
        if(lcResp.getStatus().equals(1)) {
            stamp=TimeUtil.getNowTimeStamp();
            lcde.setStamp(stamp);
            request = new HttpEntity<>(JSONObject.toJSONString(lcde), headers);
            resp = restTemplate.postForObject(leCaiParams.getDepUrl(), request, String.class);
            lcResp = JSONObject.parseObject(resp, LeCaiDepResp.class);
        }
        if(lcResp.getStatus().equals(0)){
            List<DepResultContent> resultContent=lcResp.getResultContent();
            //如果部门列表有值
            if(resultContent.size()>0){
                //删除tmp_dt_dep表
                leCaiDepUserService.deleteTmpDtDep();
            }
            System.out.println("从乐才取得的部门数量=="+resultContent.size());
            //循环部门列表
            for (Iterator iterator2 = resultContent.iterator(); iterator2.hasNext();) {
                DepResultContent depResultContent = (DepResultContent) iterator2.next();
                TmpDtDepReq tddr=new TmpDtDepReq();
                tddr.setDep_no_add(depResultContent.getDepartId().toString());
                tddr.setDep_no_parent(depResultContent.getParentId().toString());
                tddr.setDep_name(depResultContent.getDepartName());
                ///循环插入tmp_dt_dep
                leCaiDepUserService.insertTmpDtDep(tddr);
            }
        }else {
            System.out.println("同步部门返回错误信息："+lcResp.getErrorMsg());
        }
        //调用同步部门的存储过程
        CommProcEntity commProcEntity = new CommProcEntity();
        commProcEntity.setProcName("updateDep");
        commProcService.loadProcData(commProcEntity);
    }
}
